package com.abc.service;

import com.abc.bean.Stock;
import com.abc.repository.StockMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-07-23
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {


    @Autowired
    StockMapper stockMapper;

    @Transactional
    @Override
    public boolean addStock(Stock stock) {
        Stock s = stockMapper.findByName(stock.getName());
        if (s == null) {
            stockMapper.insert(stock);
            return true;
        } else {
            s.setTotal(s.getTotal() + stock.getTotal());
            stockMapper.updateById(s);
        }
        return true;
    }

    @Override
    public Stock getStockByName(String name) {
        return stockMapper.findByName(name);
    }
}
